/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * - Worker thread that actually pushes the replication data to another replication server. 
 * - The connection is not persistent, as the thread attempts to connect to an IP address, and if it succeeds, it will send the message.
 * @author BrandonNeal
 */
public class ReplicatorPusher extends Thread {
    
    private Socket s;
    private String ip;
    private ObjectOutputStream out;
    private String Message;
    
    private int TIMEOUT;
    private int PORT;
    
    public ReplicatorPusher(String ip, String Message, int timeout, int port)
    {
        this.ip = ip;
        this.Message = Message;
        this.TIMEOUT = timeout;
        this.PORT = port;
        this.start();
        
    }
    
    @Override
    public void run()
    {
        try
        {
            connect();
            System.out.println("Sending "+this.Message+" to "+this.ip);
            this.out.writeObject(this.Message);
        } 
        catch (IOException ex) 
        {
            Logger.getLogger(ReplicatorPusher.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    private void connect() throws IOException
    {
      SocketAddress sockaddr = new InetSocketAddress(this.ip, this.PORT);
      Socket sock = new Socket();
      sock.connect(sockaddr, this.TIMEOUT);
      out = new ObjectOutputStream(sock.getOutputStream());
    }
}
